<template>
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <!-- 筛选区 -->
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a @click="changeBrand(brand.id)"  href="JavaScript:;" :class="{active:filterData.brands.selectedBrand===brand.id}" v-for="brand in filterData.brands" :key="brand.id">{{ brand.name }}</a>
      </div>
    </div>
    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head"> {{ p.name }}</div>
      <div class="body">
        <a @click="changeAttr(p,attr.id)" href="javascript:;" :class="{active:p.selectedProp===attr.id}" v-for="attr in p.properties" :key="attr.id">{{ attr.name }}</a>
      </div>
    </div>
  </div>

  <!-- 白屏骨架 -->
  <div v-else class="sub-filter">
    <xtxSkeleton class="item" width="800px" height="40px"  />
    <xtxSkeleton class="item" width="800px" height="40px"  />
    <xtxSkeleton class="item" width="600px" height="40px"  />
    <xtxSkeleton class="item" width="600px" height="40px"  />
    <xtxSkeleton class="item" width="600px" height="40px"  />
  </div>
</template>

<script>
import { findSubCategoryFilter } from '@/api/category'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    // 1.获取数据
    // 2.数据中需要全部选中，需要预览将来点击激活功能，默认选中全部
    // 3.完成渲染
    const route = useRoute()
    const filterData = ref(null) // 存储数据

    // 加载中状态
    const filterLoading = ref(false)

    // 4.分类发生改变的时候需要重新获取筛选数据，需要使用侦听器
    watch(() => route.params.id, (newVal, oldVal) => {
      // 当你从二级分类去顶级分类也会拿到id，不能加载数据是因为他是二级分类的id
      if (newVal && route.path === ('/category/sub/' + newVal)) {
        filterLoading.value = true

        newVal && findSubCategoryFilter(route.params.id).then(({ result }) => {
          // 品牌的全部选项
          result.brands.selectedBrand = null
          result.brands.unshift({ id: null, name: '全部' })

          console.log(result, '5')
          // 销售属性的全部
          result.saleProperties.forEach(p => {
            p.selectedProp = null
            p.properties.unshift({ id: null, name: '全部' })
          })
          // 赋值
          filterData.value = result
          // 关闭加载中开关
          filterLoading.value = false
        })
      }
    }, { immediate: true })

    //  获取筛选参数
    const getFilterParams = () => {
      // 组装筛选参数
      const obj = { brandId: null, attrs: [] }

      // 将点击的商品品牌id赋值给参数对象中
      obj.brandId = filterData.value.brands.selectedBrand
      console.log(filterData.value, '2')

      // 遍历商品属性
      filterData.value.saleProperties.forEach(p => {
        if (p && p.selectedProp) {
          // 遍历筛选匹配属性对象id与点击的属性id 然后push到参数对象中
          const prop = p.properties.find(prop => prop.id === p.selectedProp)
          obj.attrs.push({ groupName: p.name, propertyName: prop.name })
        }
      })

      // 参考数据：{brandId:'',attrs:[{groupName:'',propertyName:''},...]}
      if (obj.attrs.length === 0) obj.attrs = null

      return obj
    }
    // 选择品牌
    const changeBrand = (BrandId) => {
      // 判断点击的品牌有重复点击的行为，如果是的话则返回
      if (filterData.value.brands.selectedBrand === BrandId) return
      filterData.value.brands.selectedBrand = BrandId
      // 触发父组件的事件
      emit('filter-change', getFilterParams())
    }

    // 选择属性
    const changeAttr = (p, attrId) => {
      // 判断点击的属性有重复点击的行为，如果是的话则返回
      if (p.selectedProp === attrId) return
      p.selectedProp = attrId
      // 触发父组件的事件
      emit('filter-change', getFilterParams())
    }
    // 返回数据
    return { filterData, filterLoading, changeBrand, changeAttr }
  }
}
</script>

<style lang="less" scoped>
.sub-filter {
  background: #fff;
  padding: 25px;
  .xtx-skeleton {
    padding: 10px 0;
  }
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,&:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
